import time
# /********************金逸晨**************************
# *****************1.3 4PIN IIC OLED FOR Arduino*************
# *****3----3SCK,   4----4SDA,
# *****BY:GU
#   1.3寸旋转90度显示，结合取模方式，及翻转命令完成旋转显示
#   *A0,C0 完成180读旋转
# ********************************************************/
OLED_COLUMN_NUMBER= 128
OLED_LINE_NUMBER= 64
OLED_COLUMN_OFFSET= 2
OLED_PAGE_NUMBER= (OLED_LINE_NUMBER/8)
IIC_SCK= 8
IIC_SDA= 7


const unsigned char *point;

OLED_init_cmd =const([
#   关闭显示
  0xAE,
# //设置时钟分频因子,震荡频率
  0xD5,
#   //[3:0],分频因子;[7:4],震荡频率
  0x80,  
# //设置驱动路数
  0xA8,
#   //默认(1/64)
  0X3F,
# //设置显示偏移
  0xD3,
#   //默认为0
  0X00,
#   //设置显示开始行 [5:0],行数.

  0x40,
#   //电荷泵设置
  0x8D,
#   //bit2，开启/关闭
  0x14,
#   //设置内存地址模式
  0x20,
#   //[1:0],00，列地址模式;01，行地址模式;10,页地址模式;默认10;
  0x02,
#   /************显示翻转命令**********************************************/
#   //段重定义设置,bit0:0,0->0;1,0->127;  A1
  0xA1,
# //设置COM扫描方向;bit3:0,普通模式;1,重定义模式 COM[N-1]->COM0;N:驱动路数 (C0 翻转显示) C8
  0xC8,
#   /********************************************************************************************************/
#   //设置COM硬件引脚配置
  0xDA,
#   //[5:4]配置
  0x12,
# //对比度设置
  0x81,
#   //1~255;默认0X7F (亮度设置,越大越亮)
  0x66,
# //设置预充电周期
  0xD9,
#   //[3:0],PHASE 1;[7:4],PHASE 2;
  0xf1,
# //设置VCOMH 电压倍率
  0xDB,
#   //[6:4] 000,0.65*vcc;001,0.77*vcc;011,0.83*vcc;
  0x30,
# //全局显示开启;bit0:1,开启;0,关闭;(白屏/黑屏)
  0xA4,
# //设置显示方式;bit0:1,反相显示;0,正常显示
  0xA6,
# //开启显示
  0xAF
]);
 
picture_tab=[
#   /*------------------------------------------------------------------------------
#     ;  列行式，低位在前，阴码
#     ;  宽×高（像素）: 128×64
#     ------------------------------------------------------------------------------*/
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0xC0,
  0x80, 0xC0, 0xC0, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0xC0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xE0, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00, 0x80, 0x00, 0x00, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0,
  0xC0, 0xC0, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x06, 0xCC,
  0x40, 0x15, 0x37, 0xF3, 0x73, 0xFC, 0xFC, 0xF3, 0xB6, 0x35, 0x31, 0x00, 0x00, 0x00, 0x82, 0x82,
  0xFF, 0xC2, 0x42, 0xC0, 0xFF, 0x00, 0x00, 0xFF, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0x70, 0xF0, 0xF8, 0xF8, 0xFC, 0xFC, 0xF6, 0xF6, 0xF7,
  0xF3, 0xF3, 0xF3, 0xF1, 0x71, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x01, 0x81, 0xC1, 0xC1, 0xE1, 0xE3, 0xE3, 0xF7, 0xF6, 0xF6, 0xFC, 0xFC, 0xF8, 0xF8,
  0xF0, 0xF0, 0xE0, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x07, 0x01,
  0x00, 0x06, 0x03, 0x01, 0x00, 0x0F, 0x07, 0x00, 0x01, 0x03, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00,
  0x00, 0x00, 0x06, 0x03, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x80, 0xF0, 0xFC, 0x1E, 0x07, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF,
  0xFF, 0x00, 0x38, 0x78, 0x78, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0x38, 0x00, 0x00, 0x00, 0x00, 0x78,
  0xF8, 0xF8, 0xFE, 0xFF, 0xFF, 0x7F, 0x7B, 0x39, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x07,
  0x7F, 0xFF, 0xFF, 0xFB, 0x07, 0x1E, 0x7C, 0xF0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
  0xC0, 0xC0, 0x60, 0xF0, 0xF8, 0xDC, 0xCE, 0xC7, 0xC7, 0xCC, 0xD8, 0xF8, 0xF0, 0x60, 0x60, 0xC0,
  0xC0, 0x00, 0x00, 0x00, 0x80, 0x86, 0x9E, 0x98, 0x00, 0x18, 0xFC, 0xFE, 0x37, 0x37, 0xF6, 0xFE,
  0x3E, 0x36, 0x30, 0xF0, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xFF, 0xFF, 0xFD, 0xFD,
  0xFD, 0xFD, 0xFD, 0xFD, 0xFD, 0xFD, 0xFD, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x3F, 0xFF, 0xE0, 0xF0, 0xF0, 0xF8, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF,
  0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x0F, 0x3F, 0xFF, 0xFE, 0xF8, 0xE0, 0xE0, 0xF8, 0xFE,
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x80, 0x80, 0x80, 0x00, 0x00, 0xC0, 0xFF, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x01, 0x04, 0x0C, 0x7C, 0xEC, 0xCC, 0x0C, 0xFF, 0xFF, 0x0C, 0x8C, 0xEC, 0x7C, 0x1C, 0x0C, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x01, 0x81, 0xFF, 0xFF, 0x80, 0xA0, 0xE1, 0x71, 0x39, 0x1F, 0x0F, 0x1F,
  0x7F, 0x67, 0x6F, 0x69, 0x71, 0xB0, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xFF, 0x3F, 0x18, 0xFA, 0xFA,
  0x9A, 0x9A, 0xFA, 0x7A, 0xFA, 0xDA, 0xFA, 0xBA, 0x1A, 0x18, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x01, 0x07, 0x3F, 0x3F, 0x7F, 0x7F, 0xF8, 0xF8, 0xF8, 0x78, 0x78, 0x3C, 0x3F, 0x1F, 0x1F,
  0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0x03,
  0x00, 0x01, 0x07, 0x0F, 0x1F, 0x3F, 0x7E, 0x78, 0x78, 0xF0, 0xF0, 0xF0, 0xF0, 0x70, 0x78, 0xFC,
  0xFE, 0xFF, 0x7F, 0x7F, 0x1F, 0x0F, 0x07, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x03,
  0x00, 0x00, 0x00, 0xC0, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x83,
  0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x80, 0x80, 0x80, 0x81, 0x83, 0x01, 0x00, 0x00, 0x03, 0x03,
  0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x03, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x06, 0x06, 0x0C, 0x0C, 0x1C,
  0x18, 0x18, 0x38, 0x30, 0x30, 0x30, 0x30, 0x7C, 0x7E, 0x7E, 0x7E, 0x7F, 0x7F, 0x7F, 0x7F, 0x7E,
  0x7E, 0x7E, 0x3C, 0x30, 0x30, 0x30, 0x30, 0x18, 0x18, 0x1C, 0x0C, 0x0C, 0x06, 0x06, 0x03, 0x03,
  0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0x92,
  0x92, 0x92, 0xBB, 0xFF, 0x93, 0x92, 0x92, 0xFE, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30,
  0x30, 0x30, 0x30, 0x30, 0xFC, 0xFE, 0x36, 0x33, 0x31, 0x31, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x01,
  0x01, 0x01, 0x01, 0x0F, 0x09, 0x09, 0x09, 0x09, 0x09, 0x0C, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x08, 0x08, 0x0F, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
]



def delay_us(time_us):
  time.sleep(time_us)
  

# /**************************IIC模块发送函数************************************************
# 
#  *************************************************************************/
# //写入  最后将SDA拉高，以等待从设备产生应答
def  IIC_write(unsigned char date):
  unsigned char i, temp;
  temp = date;
  for (i = 0; i < 8; i++)
  { digitalWrite(IIC_SCK, LOW);
    
    if ((temp & 0x80) == 0)
      digitalWrite(IIC_SDA, LOW);
    else digitalWrite(IIC_SDA, HIGH);
    temp = temp << 1;
#     //最少250ns延时
    digitalWrite(IIC_SCK, HIGH);
  }
  digitalWrite(IIC_SCK, LOW)
  digitalWrite(IIC_SDA, HIGH)
  digitalWrite(IIC_SCK, HIGH)
#   //不进行应答检测
  digitalWrite(IIC_SCK, LOW)
  


 
# //启动信号
# //SCL在高电平期间，SDA由高电平向低电平的变化定义为启动信号
def  IIC_start()
{
  digitalWrite(IIC_SDA, HIGH);
  
  digitalWrite(IIC_SCK, HIGH);
  //所有操作结束释放SCL
  
  digitalWrite(IIC_SDA, LOW);
  
  digitalWrite(IIC_SCK, LOW);
  
  IIC_write(0x78);

}

# //停止信号
# //SCL在高电平期间，SDA由低电平向高电平的变化定义为停止信号
def  IIC_stop()
{
  digitalWrite(IIC_SDA, LOW);
  
  digitalWrite(IIC_SCK, HIGH);
  
  digitalWrite(IIC_SDA, HIGH);
  digitalWrite(IIC_SCK, LOW);
  digitalWrite(IIC_SDA, LOW);
  
}

def  OLED_send_cmd(unsigned char o_command)
{

  IIC_start();
  IIC_write(0x00);
  IIC_write(o_command);

  IIC_stop();

}


def  OLED_send_data(unsigned char o_data)
{
  IIC_start();
  IIC_write(0x40);
  IIC_write(o_data);

  IIC_stop();
}


def  Column_set(unsigned char column)
{
  column = column +OLED_COLUMN_OFFSET;
  OLED_send_cmd(0x10 | (column >> 4)); //设置列地址高位
  OLED_send_cmd(0x00 | (column & 0x0f)); //设置列地址低位
}


def  Page_set(unsigned char page)
{
  OLED_send_cmd(0xb0 + page);
}


def  OLED_clear(void)
{
  unsigned char page, column;
  for (page = 0; page < OLED_PAGE_NUMBER; page++)      //page loop
  {
    Page_set(page);
    Column_set(0);
    for (column = 0; column < OLED_COLUMN_NUMBER; column++) //column loop
    {
      OLED_send_data(0x00);
    }
  }
}
def  OLED_full(void)
{
  unsigned char page, column;
  for (page = 0; page < OLED_PAGE_NUMBER; page++)      //page loop
  {
    Page_set(page);
    Column_set(0);
    for (column = 0; column < OLED_COLUMN_NUMBER; column++) //column loop
    {
      OLED_send_data(0xff);
    }
  }
}
def  OLED_init(void)
{
  unsigned char i;
  for (i = 0; i < 25; i++)
  {
    OLED_send_cmd(OLED_init_cmd[i]);
  }
}

def  Picture_display(const unsigned char *ptr_pic)
{
  unsigned char page, column;
  for (page = 0; page < (OLED_LINE_NUMBER/8); page++) //page loop
  {
    Page_set(page);
    Column_set(0);
    for (column = 0; column < OLED_COLUMN_NUMBER; column++) //column loop
    {
      OLED_send_data(*ptr_pic++);
    }
  }
}
def  Picture_ReverseDisplay(const unsigned char *ptr_pic)
{
  unsigned char page, column, data;
  for (page = 0; page < (OLED_LINE_NUMBER/8); page++) //page loop
  {
    Page_set(page);
    Column_set(0);
    for (column = 0; column < OLED_COLUMN_NUMBER; column++) //column loop
    {
      data = *ptr_pic++;
      data = ~data;
      OLED_send_data(data);
    }
  }
}


def IO_init(void)
# 设置数字脚为输出
  pinMode(IIC_SCK, OUTPUT)
# 设置数字脚为输出
  pinMode(IIC_SDA, OUTPUT)
  digitalWrite(IIC_SCK, LOW
  digitalWrite(IIC_SDA, LOW
 


point = &picture_tab[0];
IO_init();
OLED_init();
OLED_full();
delay(3000);
OLED_clear();
delay(3000);
Picture_display(point);
delay(3000);
 

